<template>
  <el-dialog width="400" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <el-form class="dialog-form" :model="formData" ref="vForm" label-position="right" label-width="80px" size="medium"
      @submit.native.prevent :rules="formRules">
      <el-form-item label="版本号" prop="verNo">
        <el-input v-model="formData.verNo" :disabled="type === 'edit'" placeholder="请输入版本号" link clearable />
      </el-form-item>
      <el-form-item label="协议类型" prop="protocol">
        <el-select v-model="formData.protocol" placeholder="请选择协议类型" class="full-width-input" clearable>
          <el-option v-for="(item, index) in protocolOptions" :key="index" :label="item.label" :value="item.value"
            :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" placeholder="请输入描述" type="textarea" :rows="2" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update, getInfo } from '@/api/bas/brandVer'
export default {
  name: 'brandVerDialog',
  data() {
    return {
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      formDisabled: false,
      formRules: {
        verNo: [{ required: true, message: '请输入版本号', trigger: 'blur' }],
        protocol: [{ required: true, message: '请选择协议类型', trigger: 'change' }],
      },
      protocolOptions: [
        {
          label: 'http',
          value: 0,
        },
        {
          label: 'https',
          value: 1,
        },
        {
          label: 'socket客户端',
          value: 2,
        },
        {
          label: 'socket服务端',
          value: 3,
        },
        {
          label: 'websocket客户端',
          value: 4,
        },
        {
          label: 'websocket服务端',
          value: 5,
        },
      ],
    }
  },
  created() { },
  methods: {
    add(brandCode) {
      this.formDisabled = false
      this.type = 'add'
      this.dialogTitle = '新增版本'
      // 先清空表单数据
      this.formData = {
        brandCode: brandCode,
      }
      this.dialogVisible = true
    },
    edit(row) {
      this.formDisabled = true
      this.type = 'edit'
      this.dialogTitle = '编辑版本'
      getInfo(row.verId)
        .then((res) => {
          if (res.data.code === 200) {
            this.formData = res.data.data
            this.dialogVisible = true
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    submitForm() {
      this.$refs['vForm'].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            add(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('新增成功')
                  this.closeDialog()
                  this.$emit('ok', this.formData)
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          } else {
            update(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('编辑成功')
                  this.closeDialog()
                  this.$emit('ok', this.formData)
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          }
        }
      })
    },
    closeDialog() {
      this.$refs['vForm'].resetFields()
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped>
.dialog-form {
  padding: 0 30px;
  /* 根据需求调整数值 */
}
</style>
